type ObserverFunc = (data: any) => void;

class Observable {
  public observers: ObserverFunc[];

  constructor() {
    this.observers = [];
  }

  subscribe(func: ObserverFunc) {
    this.observers.push(func);
  }

  unsubscribe(func: ObserverFunc) {
    this.observers = this.observers.filter((subscriber) => {
      subscriber !== func;
    });
  }

  notify(data: any) {
    this.observers.forEach((observer) => {
      observer(data);
    });
  }
}

export default new Observable();
